<template>
  <div class="modal" v-show="$store.state.isOpenLogin">
    <div class="modal_bg" @click="closeLogin"></div>
    <div class="modal_content">
      <img
        class="close"
        @click="closeLogin"
        src="../assets/images/login/userArrow.png"
        alt=""
      />
      <section>
        <span :class="{ active: showPhone }" @click="showPhone = true"
          >手机号登录</span
        >
        <span :class="{ active: !showPhone }" @click="wechatLogin"
          >微信扫码登录</span
        >
      </section>
      <div class="phoneLogin" v-show="showPhone">
        <div class="tanxin line">
          <input
            type="text"
            maxlength="11"
            placeholder="请输入手机号"
            v-model="phonVal"
          />
        </div>
        <!-- 滑块验证码 -->
        <div class="verify_box">
          <slide-verify
            :l="42"
            :r="10"
            :w="360"
            :h="155"
            slider-text="向右滑动"
            @success="onSuccess"
            @fail="onFail"
            @refresh="onRefresh"
          ></slide-verify>
          <div>{{ msg }}</div>
        </div>

        <div class="tanxin line">
          <input type="text" placeholder="请输入验证码" v-model="texVal" />
          <div class="btn" @click="getYanzhen" v-if="isShowYanzhen">
            获取验证码
          </div>
          <div class="daojishi" v-else>{{ count }}</div>
        </div>
        <div class="btn_login" @click="login">登录</div>
      </div>
      <div id="weixin" v-show="!showPhone"></div>
    </div>
  </div>
</template>

<script>
import { getCodeApi, LoginApi, bindWechatPhoneApi } from "@/request/api";
const vaildatephone = (phonVal) => {
  let reg =
    /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
  return reg.test(phonVal.trim());
};
export default {
  data() {
    return {
      //微信还是手机号登录
      showPhone: true,
      msg: "",
      //获取 手机号
      phonVal: "13541726626",
      //获取验证码
      texVal: "9844",
      //拼图
      codeFlag: false,
      // 倒计时
      count: 3,
      isShowYanzhen: true,
    };
  },
  methods: {
    closeLogin() {
      //关闭模态框
      this.$store.commit("changeIsLogin", false);
    },
    onSuccess() {
      this.msg = "验证成功";
      this.codeFlag = true;
    },
    onFail() {
      this.msg = "验证失败";
      this.codeFlag = false;
    },
    onRefresh() {
      this.msg = "";
    },
    wechatLogin() {
      this.showPhone = false;
      let _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa(process.env.VUE_APP_STATE_URL + _this.$route.path)
        ),
        // 调用样式文件
        href: "data:text/css;base64,Lyogd3hsb2dpbi5jc3MgKi8NCi5pbXBvd2VyQm94IC50aXRsZSwNCi5pbXBvd2VyQm94IC5pbmZvIHsNCiAgICBkaXNwbGF5OiBub25lOw0KfQ0KDQouaW1wb3dlckJveCAucXJjb2RlIHsNCiAgICBtYXJnaW4tdG9wOiAyMHB4Ow0KfQ==",
      });
    },
    login() {
      //验证手机号格式
      let result = vaildatephone(this.phonVal);
      if (!result) {
        let modalObj = {
          LogMsg: "请输入正确的手机号",
          icon: "danger",
        };
        this.$store.dispatch("AsyncModal", modalObj);
        return;
      }
      // 拼图验证的
      if (!this.codeFlag) {
        let modalObj = {
          LogMsg: "请完成拼图再进行下一步",
          icon: "danger",
        };
        this.$store.dispatch("AsyncModal", modalObj);
        return;
      }

      // 校验验证码是否输出的功能
      let mycode = this.texVal.trim();
      if (mycode == "") {
        let modalObj = {
          LogMsg: "请输入验证码在进行下一步 ",
          icon: "danger",
        };
        this.$store.dispatch("AsyncModal", modalObj);
        return;
      }
      // 刚才我们知道  如果存在 uuid 的东西 ，就是微信扫码登录得到的
      // 如果不存在 uuid  就是 手机号登录

      // 存在uuid
      let uuid = sessionStorage.getItem("uuid");
      if (uuid) {
        // 如果存在 uuid 的东西 ，证明就是微信扫码登录得到的
        bindWechatPhoneApi({
          phone: this.phonVal,
          verifyCode: this.texVal,
          uuid: uuid,
        }).then((res) => {
          console.log(res);
          if (res.data.code === 0) {
            // 已经登录成功
            // 存储token
            localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
            // 提示登录成功
            let modalObj = {
              LogMsg: res.data.message,
              icon: "success",
            };
            this.$store.dispatch("AsyncModal", modalObj);
            // 关闭登录框
            this.$store.commit('changeIsLogin',false)
            // 更新header组件
            this.$emit("updateKeyFn");
            // 删除地址栏的code
            this.$router.push(this.$route.path)
            // 删除uuid
            sessionStorage.removeItem('uuid')
          }
        });
      } else {
        // //发送请求
        LoginApi({
          verifyCode: this.texVal,
          phone: this.phonVal,
        }).then((res) => {
          // console.log(res);
          if (res.data.code === 0) {
            // 存储token
            localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
            // 提示登录成功
            let modalObj = {
              content: res.data.message,
              icon: "success",
            };
            this.$store.dispatch("AsyncModal", modalObj);
            // 关闭登录框
            this.$store.commit("changeIsLogin", false);
          }
        });
      }
    },
    //倒计时

    getYanzhen() {
      // 验证手机号格式
      let result = vaildatephone(this.phonVal);
      if (!result) {
        let modalObj = {
          LogMsg: "请输入正确的手机号",
          icon: "danger",
        };
        this.$store.dispatch("AsyncModal", modalObj);
        return;
      }
      // 拼图验证的
      if (!this.codeFlag) {
        let modalObj = {
          LogMsg: "请完成拼图再进行下一步",
          icon: "danger",
        };
        this.$store.dispatch("AsyncModal", modalObj);
        return;
      }

      // 获取了验证码 ----> 倒计时功能
      getCodeApi({ phone: this.phonVal }).then((res) => {
        if (res.data.code === 0) {
          let modalObj = {
            LogMsg: res.data.message,
            icon: "success",
          };
          this.$store.dispatch("AsyncModal", modalObj);
          // 显示计时器
          this.isShowYanzhen = false;
          // 获取验证码的数据
          let timer = setInterval(() => {
            this.count--;
            if (this.count === 0) {
              // 停止计时
              clearInterval(timer);
              this.count = 3;
              // 显示 '正常按钮'
              this.isShowYanzhen = true;
            }
          }, 1000);
        } else {
          let modalObj = {
            LogMsg: res.data.message,
            icon: "danger",
          };
          this.$store.dispatch("AsyncModal", modalObj);
        }
      });
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/base.less";
.modal {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  .modal_bg {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.4);
  }
  .modal_content {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    width: 555px;
    height: 423px;
    background: url(../assets/images/login/loginBg.png);
    .close {
      position: absolute;
      top: 20px;
      right: 60px;
    }
    section {
      display: flex;
      justify-content: center;
      padding-top: 50px;
      margin-bottom: 30px;
      span {
        color: #ccc;
        cursor: pointer;
        &:nth-of-type(1) {
          border-right: 1px solid #ccc;
          padding-right: 10px;
          margin-right: 10px;
        }
        &.active {
          color: #333;
        }
      }
    }
    .line {
      width: 360px;
      margin: 0 auto 20px;
      height: 50px;
      input {
        flex: 1;
        text-indent: 1em;
        outline: none;
      }
      .btn {
        width: 90px;
        height: 50px;
        text-align: center;
        background: @blue;
        color: #fff;
        cursor: pointer;
        line-height: 50px;
        margin-left: 10px;
      }
      .daojishi {
        width: 90px;
        height: 50px;
        text-align: center;
        background: #ccc;
        color: #fff;
        cursor: pointer;
        line-height: 50px;
        margin-left: 10px;
      }
    }
    .btn_login {
      width: 360px;
      height: 50px;
      text-align: center;
      background: @blue;
      color: #fff;
      line-height: 50px;
      margin: auto;
    }
    .verify_box {
      width: 360px;
      margin: 0 auto 20px;
      /deep/.slide-verify {
        width: 100% !important;
        canvas {
          position: absolute;
          top: -150px;
          left: 0;
          display: none;
          box-sizing: border-box;
        }
        .slide-verify-refresh-icon {
          position: absolute;
          top: -150px;
          display: none;
        }
        &:hover {
          canvas {
            display: block;
          }
          .slide-verify-refresh-icon {
            display: block;
          }
        }
      }
    }
  }
}
#weixin {
  width: 300px;
  /* height: 300px; */
  margin: 0 auto;
  /* background-color: pink; */
}
</style>